package com.example.wochacha.mvpdemo.login.presenter;

import android.os.Handler;
import android.os.Looper;

import com.example.wochacha.mvpdemo.login.model.IUser;
import com.example.wochacha.mvpdemo.login.model.UserModel;
import com.example.wochacha.mvpdemo.login.view.ILoginView;

import javax.inject.Inject;


/**
 * Created by josan on 2017/12/1.
 */

public class LoginPresenterCompl implements ILoginPresenter {
    ILoginView iLoginView;
    IUser user;
    Handler handler;

    /**
     * 构造函数上加上@Inject,这样，LoginActivity中的loginPresenter便与这个构造函数建立了某种联系
     * 这种联系我们可以理解为，当看到某个类被@Inject标记时，就会到它到构造方法中
     * 如果这个构造方法，也被@Inject标记时，就会自动初始化这个类，从而完成依赖注入
     * @param iLoginView
     */
    @Inject
    public LoginPresenterCompl(ILoginView iLoginView) {
        this.iLoginView = iLoginView;
        initUser();
        handler = new Handler(Looper.getMainLooper());
    }

    private void initUser() {
        user = new UserModel("mvp", "mvp");
    }

    @Override
    public void clear() {
        iLoginView.onClearText();
    }

    @Override
    public void doLogin(String name, String pwd) {
        Boolean isLoginSuccess = true;
        final int code = user.checkUserValidity(name, pwd);
        if (code != 0) {
            isLoginSuccess = false;
        }
        final Boolean finalIsLoginSuccess = isLoginSuccess;
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                iLoginView.onLoginResult(finalIsLoginSuccess, code);
            }
        }, 3000);
    }

    @Override
    public void setProgressBarVisiblity(int visiblity) {
        iLoginView.onSetProgressBarVisibility(visiblity);
    }
}
